<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Share extends CI_Controller {
    const IV = "d89fb057f6d4f03g";//加密向量，16个字节
    const KEY = 'e9c8e878ee8e2658';//密钥，16个字节
	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see https://codeigniter.com/user_guide/general/urls.html
	 */
	/*
	 * description:分享链接
	 * author:
	 *
	 * **/
	public function getLink(){
	    $app  = $_GET['app'];
        $commodity_id = 10;
        $this->load->library('session');
        $user_id = $this->session->userdata('user_id');
        $signstr = "user_id=".$user_id."?commodity_id=".$commodity_id;
        $sign = $this->encrypt($signstr);

    }
    /**
     * 加密字符串
     * @param string $strContent 待加密的字符串内容
     * @param string $key 加密key
     * @return string 返回加密后的字符串，失败返回false
     */
    public static function encrypt($strContent,$key = self::KEY,$iv = self::IV){
        $strEncrypted = openssl_encrypt($strContent,"AES-128-CBC", $key,OPENSSL_RAW_DATA, $iv);
        return base64_encode($strEncrypted);
    }

    /**
     * 解密字符串
     * @param string $strEncryptCode加密后的字符串
     * @param string $key 加密key
     * @return string 返回解密后的字符串，失败返回false
     */
    public static function decrypt($strEncryptCode,$key = self::KEY,$iv = self::IV){
        $strEncrypted = base64_decode($strEncryptCode);
        return openssl_decrypt($strEncrypted,"AES-128-CBC",$key,OPENSSL_RAW_DATA,$iv);
    }
    function create_qrcode($qr_path,$filename,$scene,$page='',$expires_in=7200){
        if(empty($qr_path)) return array('status'=>0,'info'=>'缺少存储路径');
        if(empty($filename)) return array('status'=>0,'info'=>'请确定存储的图片名称');
        if(empty($scene)) return array('status'=>0,'info'=>'缺少二维码场景值');
        if(!is_dir('.'.$qr_path)){                              //  ./Public/Qrcode/
            mkdir(iconv("GBK","UTF-8",'.'.$qr_path),0777,true);
        }
        $file = $qr_path.$filename;                             //   /Public/Qrcode/aaa.png
        $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
        $fileUrl = $protocol.$_SERVER['HTTP_HOST'].$file;       //   http://yourhost/Public/Qrcode/aaa.png
        $savePath = '.'.$file;                                  //   ./Public/Qrcode/aaa.png
        if(file_exists($savePath)){
            //当前时间-文件创建时间<过期时间
            if( (time()-filemtime($savePath)) < $expires_in ) return array('status'=>1,'info'=>$fileUrl);
        }
        $accessToken = 'xxxxxxxxxxxxxxxxxxxxxx';                // 获取到的 access_token
        $url = 'https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token='.$accessToken;
        $qrcode = array(

            'scene'         => $scene,

            'width'         => 200,

            'page'          => $page,

            'auto_color'    => true

        );
        $result = request($url,true,'POST',json_encode($qrcode));
        $errcode = json_decode($result,true)['errcode'];
        $errmsg = json_decode($result,true)['errmsg'];
        if($errcode) return array('status'=>0,'info'=>$errmsg);
        $res = file_put_contents($savePath,$result);            //  将获取到的二维码图片流保存成图片文件
        if($res===false) return array('status'=>0,'info'=>'生成二维码失败');
        return array('status'=>1,'info'=>$fileUrl);           //返回本地图片地址
    }
}
